# 最小二乘回归树分类算法
import numpy as np

try:
    data = np.genfromtxt('LearningData2.csv', delimiter=',',skip_header=1,dtype=str,encoding='utf-8')
except:
    data = np.genfromtxt('Works/第5章特征选择/LearningData2.csv', delimiter=',',skip_header=1,dtype=str,encoding='utf-8')

def mean_squared_error(data , predictions):
    squared_error = 0
    data1 = []
    data2 = []
    for i in range(len(data)):
        if float(data[i][0]) < predictions:
            data1.append(data[i])
        elif float(data[i][0]) > predictions:
            data2.append(data[i])

    c1 = np.mean([float(x[-1]) for x in data1])
    c2 = np.mean([float(x[-1]) for x in data2])

    for i in range(len(data1)):
        squared_error += (float(data1[i][-1]) - c1) ** 2

    for i in range(len(data2)):
        squared_error += (float(data2[i][-1]) - c2) ** 2

    return squared_error 

# 选择最佳分割点
def choose_best_split(data):
    best_mse = float('inf')
    best_split = None
    for predictions in np.unique(data[:, 0].astype(float)):
        mse = mean_squared_error(data, predictions)
        if mse < best_mse:
            best_mse = mse
            best_split = predictions
    return best_split, best_mse

print("根节点最佳分割点为:", choose_best_split(data)[0])
print("左子树均值为:", np.mean([float(x[-1]) for x in data if float(x[0]) < choose_best_split(data)[0]]))
print("右子树均值为:", np.mean([float(x[-1]) for x in data if float(x[0]) > choose_best_split(data)[0]]))

data_1 = []
data_2 = []
for i in range(len(data)):
    if float(data[i][0]) < choose_best_split(data)[0]:
        data_1.append(data[i])
    elif float(data[i][0]) > choose_best_split(data)[0]:
        data_2.append(data[i])

print("左子树最佳分割点为:", choose_best_split(np.array(data_1))[0])
print("左子树的左子树均值为:", np.mean([float(x[-1]) for x in data_1 if float(x[0]) < choose_best_split(np.array(data_1))[0]]))
print("左子树的右子树均值为:", np.mean([float(x[-1]) for x in data_1 if float(x[0]) > choose_best_split(np.array(data_1))[0]]))

print("右子树最佳分割点为:", choose_best_split(np.array(data_2))[0])
print("右子树的左子树均值为:", np.mean([float(x[-1]) for x in data_2 if float(x[0]) < choose_best_split(np.array(data_2))[0]]))
print("右子树的右子树均值为:", np.mean([float(x[-1]) for x in data_2 if float(x[0]) > choose_best_split(np.array(data_2))[0]]))